package com.yanqu.road.server.cmd.activity.unionwar;


import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.entity.activity.unionwar.UnionWarDispatchDetail;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.entity.enums.eUnionWarWinMark;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.logic.pb.UnionWarActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.unionwar.UnionWarActivityModule;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_WAR_BATTLE_REPORT_READ,desc = "玩家查看本商会战报")
public class UnionWarBattleReportReadCmd extends AbstractCommand {

    private int activityId;
    private int phase;
    private int session;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UnionBattleReportReadReqMsg reqMsg = UnionWarActivityServerProto.UnionBattleReportReadReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        session = reqMsg.getSession();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWarMap().get(activityId);
        //先看分组
        String unionUid = UnionWar.getUserUnionUid(activityId, player.getUserInfo());
        if(unionWar != null && !StringUtils.isNullOrEmpty(unionUid)){
            CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
            if(currentTimeInfo.getTimeInfo() != null || currentTimeInfo.getPeriodType() != null){
                UnionWarActivityServerProto.UnionBattleReportReadRespMsg.Builder respMsg = UnionWarActivityServerProto.UnionBattleReportReadRespMsg.newBuilder();
                //战报已读
                UnionWarDispatchDetail detail = unionWar.getUnionWarDispatchDetailList(unionUid, phase, session);
                if(detail != null && !detail.getReadUserId().contains(player.getUserId())){
                    synchronized (detail){
                        detail.addReadUserId(player.getUserId());
                    }
                    //UnionBaseInfo unionInfo = CrossUnionMgr.parseUnionBaseInfo(detail.getUnionUid());
                    //respMsg.setLineupInfo( UnionWarActivityPb.parseSessionLineupTempList(detail, false, unionWar.getCanDispatchMemberList(detail.getUnionUid()).size(), detail.getEnemyUnionInfo(), unionInfo));
                }

                int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, unionUid);
                UnionWarActivityServerProto.UnionBattleReportReadReqMsg.Builder crossReq = UnionWarActivityServerProto.UnionBattleReportReadReqMsg.newBuilder();
                crossReq.setActivityId(activityId);
                crossReq.setPhase(phase);
                crossReq.setSession(session);
                //转到跨服，查战报
                player.sendUnionActivityGroupPacket(Protocol.C_UNION_WAR_BATTLE_REPORT_READ, crossReq, unionWar.getActivityInfo().getActivityId(), groupId);
            }
        }
        //下发奖励
        ActivityRewardResult result = player.getModule(UnionWarActivityModule.class).getBattleResultReward(activityId, phase, session);
        if(result.getRet() == 0) {
            UnionWarActivityServerProto.UserGetBattleRewardSyncMsg.Builder respMsg = UnionWarActivityServerProto.UserGetBattleRewardSyncMsg.newBuilder();
            respMsg.setReward(result.getReward());
            respMsg.setActivityId(activityId);
            respMsg.setPhase(phase);
            respMsg.setRet(0);
            respMsg.setSession(session);
            player.sendPacket(Protocol.U_UNION_WAR_RESULT_USER_REWARD, respMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
